<?php

namespace plugin\admin\app\controller;

use support\Request;
use support\Response;
use plugin\admin\app\model\PaySet;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 支付配置 
 */
class PaySetController extends Crud
{
    
    /**
     * @var PaySet
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new PaySet;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('pay-set/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('pay-set/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('pay-set/update');
    }

    /**
     * 编辑收款码时候将本地预存的收款码一并删除，防止留存太多
     * @param Request $request
     * @return Response
     */
    public function delete_image(Request $request): Response
    {
        $data = $request->post('pushDeleteImg');
        print_r($data);
        if (empty($data)){
            return $this->json(1, 'error');
        }
        $deleteImages = str_replace('/app/admin/', '', $data);
        $isDelete = unlink(base_path() . "/plugin/admin/public/" . $deleteImages);
        return $this->json(0, $isDelete?"删除成功":"删除失败", ['data'=>$data]);
    }

}
